#include <QPainter>
#include <QPaintEvent>
#include <QPixmap>
#include <QLabel>
#include <QDebug>
#include "button.h"

Button::Button(QWidget *parent) : QPushButton(parent)
{
//    m_curIndex = 0;
//    m_background = NULL;
//    // widget 的实际尺寸只参考 sizeHint() 的返回值，不能伸展和收缩
//    setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
}

Button::Button( QString fileName, int num , QWidget *parent, int ynum, QString bkGrnd):
    QPushButton(parent)
{
    getImages(fileName, num, ynum);
    if (bkGrnd != NULL)
    {
        m_background  = new QPixmap(bkGrnd);
    }
    else
    {
        m_background = NULL;
    }
    // widget 的实际尺寸只参考 sizeHint() 的返回值，不能伸展和收缩
    setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    m_curIndex = 0;
}

Button::Button(QVector<QString> list, QWidget *parent, QString bkGrnd):
    QPushButton(parent)
{

    setPixmapList(list);
    // widget 的实际尺寸只参考 sizeHint() 的返回值，不能伸展和收缩
    setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    if (bkGrnd != NULL)
    {
        m_background  = new QPixmap(bkGrnd);
    }
    else
    {
        m_background = NULL;
    }
    m_curIndex = 0;
}

void Button::setPixmapList(QString fileName, int xNum, int yNum)
{
    m_pixmapList.clear();
    getImages(fileName, xNum, yNum);
}

void Button::setPixmapList(QVector<QString> &list)
{
    if (list.size() <= 0)
        return;
    m_pixmapList.clear();

    // 根据路径, 将图片存储到pixmapList中
    for(auto pos : list)
    {
        m_pixmapList.push_back(QPixmap(pos));
    }
}

void Button::getImages(QString fileName, int xNum, int yNum)
{
    QPixmap pixmap(fileName);
    for (int cnt = 0; cnt < xNum; ++cnt)
    {
        for (int y = 0; y < yNum; ++ y)
        {
            m_pixmapList.push_back( pixmap.copy(cnt * (pixmap.width() / xNum),      // 起始x坐标
                                               y * (pixmap.height() / yNum),        // 起始y坐标
                                               pixmap.width() / xNum ,              // 宽度
                                               pixmap.height() / yNum));            // 高度
        }
    }
}

void Button::paintEvent ( QPaintEvent * event)
{
    if(m_pixmapList.size() == 0)
        return QPushButton::paintEvent(event);
    QPainter painter(this);
    painter.drawPixmap(event->rect(), m_pixmapList[m_curIndex]);
}

// hover
void Button::enterEvent(QEvent *event)
{
    if (m_pixmapList.size() > 1)
        m_curIndex = 1;
    else
        m_curIndex = 0;
    // 刷新按钮
    update();

    QPushButton::enterEvent(event);
}

// normal - 第一张图片, index=0
void Button::leaveEvent(QEvent *event)
{
    if(!m_isToggle)
    {
        m_curIndex = 0;
        update();
    }
    QPushButton::leaveEvent(event);
}

// press - 第三张图片, index=2
void Button::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        if (m_pixmapList.size() > 2)
        {
            m_curIndex = 2;
            update();
        }
    }
    QPushButton::mousePressEvent(event);
}

// hover - 第二张图片, index=1
void Button::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton && !m_isToggle)
    {
        if (m_pixmapList.size() > 1)
        {
            m_curIndex = 1;
            update();
        }
    }
    QPushButton::mouseReleaseEvent(event);
}

QSize Button::sizeHint() const
{
    // 如果有背景图窗口大小按钮的大小参考背景图片, 如果没有则参考pixmapList中的图片设置
    return m_background != NULL ? QSize(m_background->width(), m_background->height())  :
                                QSize(m_pixmapList[0].width(), m_pixmapList[0].height());
}


